package com.longrise.android.byjk.plugins.aboutme;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.Tracker_Google;

import java.util.ArrayList;

/***
 * 培训证书
 */

public class CultivateFragmentView extends Activity {

    private ListView mListView;
    private TextView mText_BackTitle;
    private RelativeLayout mRela_Back;

    private ArrayList<EntityBean> mArrayList = new ArrayList<EntityBean>();
    private SimpleAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragmentview_traincard);
        initUI();
    }

    private void initUI() {

        mListView = (ListView) findViewById(R.id.train_listview);
        mText_BackTitle = (TextView) findViewById(R.id.app_title);
        mText_BackTitle.setText("培训证书");
        mRela_Back = (RelativeLayout) findViewById(R.id.relapp_back);
        mRela_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CultivateFragmentView.this.finish();
            }
        });

        mAdapter = new SimpleAdapter(this, mArrayList, R.layout.naturalcard_item, new String[]{"traintype", "studentno"}, new int[]{R.id.text_zh_name, R.id.text_zh_code});
        mListView.setAdapter(mAdapter);
        clientRequest();

    }

    @Override
    protected void onResume() {
        super.onResume();
        Tracker_Google.getInstance().addGoogleScreenName("trainning-certificate");
    }

    private void setData(EntityBean[] set) {
        mArrayList.clear();
        for (EntityBean bean : set) {
            mArrayList.add(bean);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void clientRequest() {
        DZZWTools.showWaitDialog(this);
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean serBean = new EntityBean();
        serBean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_user_sTrainCert", serBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                EntityBean bean = (EntityBean) result;
                if (bean != null) {
                    int value = bean.getInt("resultstate");
                    if (value == 1) {
                        EntityBean[] set = (EntityBean[]) bean.get("result");
                        if (set == null) {
                            hintDialog("没有查询到数据");
                            return;
                        }
                        setData(set);
                    } else {
                        hintDialog("没有查询到数据");
                    }
                }
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(CultivateFragmentView.this, getResources().getString(R.string.networkerror), 1000 * 2);

            }
        });
    }

    private void hintDialog(String msg) {
        View v = View.inflate(this, R.layout.dialog_connect_error, null);
        TextView text_Cancle = (TextView) v.findViewById(R.id.text_cancle_error);
        TextView textMsg = (TextView) v.findViewById(R.id.text_error);
        textMsg.setText(msg);
        final View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.text_cancle_error) {
                    CultivateFragmentView.this.finish();

                }
                DialogUtil.getInstance().dismiss();
            }
        };
        text_Cancle.setOnClickListener(onClickListener);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, v, 266, 171);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                    CultivateFragmentView.this.finish();
                }
                return false;
            }
        });
    }

}
